package com.yhw.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhw.srb.core.enums.BorrowerStatusEnum;
import com.yhw.srb.core.enums.IntegralEnum;
import com.yhw.srb.core.mapper.BorrowerAttachMapper;
import com.yhw.srb.core.mapper.UserInfoMapper;
import com.yhw.srb.core.mapper.UserIntegralMapper;
import com.yhw.srb.core.pojo.entity.Borrower;
import com.yhw.srb.core.mapper.BorrowerMapper;
import com.yhw.srb.core.pojo.entity.BorrowerAttach;
import com.yhw.srb.core.pojo.entity.UserInfo;
import com.yhw.srb.core.pojo.entity.UserIntegral;
import com.yhw.srb.core.pojo.vo.BorrowerApprovalVO;
import com.yhw.srb.core.pojo.vo.BorrowerAttachVO;
import com.yhw.srb.core.pojo.vo.BorrowerDetailVO;
import com.yhw.srb.core.pojo.vo.BorrowerVO;
import com.yhw.srb.core.service.BorrowerAttachService;
import com.yhw.srb.core.service.BorrowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhw.srb.core.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author yhw
 * @since 2022-08-29
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private BorrowerAttachMapper borrowerAttachMapper;

    @Resource
    private DictService dictService;

    @Resource
    private BorrowerAttachService borrowerAttachService;

    @Resource
    private UserIntegralMapper userIntegralMapper;

    @Override
    public void saveBorrowerVOByUserId(BorrowerVO borrowerVO, Long userId) {

        UserInfo userInfo = userInfoMapper.selectById(userId);

        //保存借款人信息
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO,borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus()); //认证中

        baseMapper.insert(borrower);

        //保存附件
        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
        for (BorrowerAttach borrowerAttach :borrowerAttachList) {
            borrowerAttach.setBorrowerId(userId);
            borrowerAttachMapper.insert(borrowerAttach);
        }

        //更新会员状态，更新为认证中
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus()); //认证中
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public Integer getStatusByUserId(Long userId) {

        LambdaQueryWrapper<Borrower> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Borrower::getUserId,userId).select(Borrower::getStatus);
        List<Object> objects = baseMapper.selectObjs(queryWrapper);

        if(objects.size() == 0){
            //借款人尚未提交信息
            return BorrowerStatusEnum.NO_AUTH.getStatus();
        }

        Integer status = (Integer)objects.get(0);
        return status;
    }

    @Override
    public IPage<Borrower> listPage(Page<Borrower> pageParam, String keyword) {

        if (StringUtils.isBlank(keyword)){
            return baseMapper.selectPage(pageParam,null);
        }

        LambdaQueryWrapper<Borrower> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Borrower::getName,keyword)
                    .or().like(Borrower::getIdCard,keyword)
                    .or().like(Borrower::getMobile,keyword)
                    .orderByDesc(Borrower::getId);

        return  baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public BorrowerDetailVO getBorrowerDetailVOById(Long id) {

        //获取借款人信息
        Borrower borrower = baseMapper.selectById(id);

        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower,borrowerDetailVO);

        //婚否
        borrowerDetailVO.setMarry(borrower.getMarry()?"是":"否");
        //性别
        borrowerDetailVO.setSex(borrower.getSex()==1?"男":"女");

        //计算下拉列表选中内容
        String education = dictService.getNameByParentDictCodeAndValue("education", borrower.getEducation());
        String industry = dictService.getNameByParentDictCodeAndValue("moneyUse", borrower.getIndustry());
        String income = dictService.getNameByParentDictCodeAndValue("income", borrower.getIncome());
        String returnSource = dictService.getNameByParentDictCodeAndValue("returnSource", borrower.getReturnSource());
        String contactsRelation = dictService.getNameByParentDictCodeAndValue("relation", borrower.getContactsRelation());

        //设置下拉列表选中内容
        borrowerDetailVO.setEducation(education);
        borrowerDetailVO.setIndustry(industry);
        borrowerDetailVO.setIncome(income);
        borrowerDetailVO.setReturnSource(returnSource);
        borrowerDetailVO.setContactsRelation(contactsRelation);

        //审批状态
        Integer status = borrower.getStatus();
        String msgByStatus = BorrowerStatusEnum.getMsgByStatus(status);
        borrowerDetailVO.setStatus(msgByStatus);

        //获取附件VO列表
        List<BorrowerAttachVO> borrowerAttachVOList =  borrowerAttachService.selectBorrowerAttachVOList(borrower.getUserId());
        borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachVOList);

        return borrowerDetailVO;
    }

    @Override
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {

        Long id = borrowerApprovalVO.getBorrowerId();
        Borrower borrower = baseMapper.selectById(id);
        borrower.setStatus(borrowerApprovalVO.getStatus());
        baseMapper.updateById(borrower);

        Long userId = borrower.getUserId();
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Integer curIntegral = userInfo.getIntegral();

        //添加积分   user_integral 表
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());   // 基本信息 获取的 积分
        userIntegral.setContent("借款人基本信息");
        userIntegralMapper.insert(userIntegral);

        curIntegral += borrowerApprovalVO.getInfoIntegral();

        if(borrowerApprovalVO.getIsIdCardOk()) {    // 身份证信息 正确 获取的积分
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegralMapper.insert(userIntegral);
            curIntegral += IntegralEnum.BORROWER_IDCARD.getIntegral();
        }

        if(borrowerApprovalVO.getIsHouseOk()) {   // 房产信息 正确 获取的积分
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegralMapper.insert(userIntegral);
            curIntegral += IntegralEnum.BORROWER_HOUSE.getIntegral();
        }

        if(borrowerApprovalVO.getIsCarOk()) {   // 车辆信息 正确 获取的积分
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegralMapper.insert(userIntegral);
            curIntegral += IntegralEnum.BORROWER_CAR.getIntegral();
        }

        userInfo.setIntegral(curIntegral);    // 用户积分
        userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());  // 借款人信息认证状态

        userInfoMapper.updateById(userInfo);
    }


}
